package com.uls.web.controller.hj;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.common.core.domain.entity.SysUser;
import com.uls.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.hj.domain.HjMessages;
import com.uls.hj.service.IHjMessagesService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 消息提醒Controller
 * 
 * @author hj
 * @date 2024-11-27
 */
@RestController
@RequestMapping("/hj/messages")
public class HjMessagesController extends BaseController
{
    @Autowired
    private IHjMessagesService hjMessagesService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询消息提醒列表
     */
    @PreAuthorize("@ss.hasPermi('hj:messages:list')")
    @GetMapping("/list")
    public TableDataInfo list(HjMessages hjMessages)
    {
        BaseController baseController = new BaseController();
        Long userId = baseController.getUserId();
        String userName = baseController.getUsername();
        String role = sysUserService.selectUserRoleGroup(userName);
        System.out.println(role);

        if (!role.equals("超级管理员")){
            hjMessages.setReceiverId(userId);
        }

        if (hjMessages.getReceiverAccount() != null){
            SysUser user = sysUserService.selectUserByUserName(hjMessages.getReceiverAccount());
            if (user != null){
                hjMessages.setReceiverId(user.getUserId());
            }else {
                hjMessages.setReceiverId(0L);
                List<HjMessages> list = hjMessagesService.selectHjMessagesList(hjMessages);
                return getDataTable(list);
            }
        }

        startPage();
        List<HjMessages> list = hjMessagesService.selectHjMessagesList(hjMessages);
        if (list != null){
            for (HjMessages hjMessage: list){
                SysUser sysUser = sysUserService.selectUserById(hjMessage.getReceiverId());
                hjMessage.setReceiverAccount(sysUser.getUserName());
            }


        }

        return getDataTable(list);
    }

    /**
     * 导出消息提醒列表
     */
    @PreAuthorize("@ss.hasPermi('hj:messages:export')")
    @Log(title = "消息提醒", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HjMessages hjMessages)
    {
        List<HjMessages> list = hjMessagesService.selectHjMessagesList(hjMessages);
        ExcelUtil<HjMessages> util = new ExcelUtil<HjMessages>(HjMessages.class);
        util.exportExcel(response, list, "消息提醒数据");
    }

    /**
     * 获取消息提醒详细信息
     */
    @PreAuthorize("@ss.hasPermi('hj:messages:query')")
    @GetMapping(value = "/{messageId}")
    public AjaxResult getInfo(@PathVariable("messageId") Long messageId)
    {
        return success(hjMessagesService.selectHjMessagesByMessageId(messageId));
    }

    /**
     * 新增消息提醒
     */
    @PreAuthorize("@ss.hasPermi('hj:messages:add')")
    @Log(title = "消息提醒", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HjMessages hjMessages)
    {
        return toAjax(hjMessagesService.insertHjMessages(hjMessages));
    }

    /**
     * 修改消息提醒
     */
    @PreAuthorize("@ss.hasPermi('hj:messages:edit')")
    @Log(title = "消息提醒", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HjMessages hjMessages)
    {
        return toAjax(hjMessagesService.updateHjMessages(hjMessages));
    }

    /**
     * 删除消息提醒
     */
    @PreAuthorize("@ss.hasPermi('hj:messages:remove')")
    @Log(title = "消息提醒", businessType = BusinessType.DELETE)
	@DeleteMapping("/{messageIds}")
    public AjaxResult remove(@PathVariable Long[] messageIds)
    {
        return toAjax(hjMessagesService.deleteHjMessagesByMessageIds(messageIds));
    }
}
